<?php
/**
 * Created by PhpStorm.
 * User: wuxiang
 * Date: 2016/7/9
 * Time: 14:53
 */

namespace Common\Libiary;
use Common\Libiary\UpYun;

//上传到U盘云
class Uploadify
{
    const UPYUN_BUCKET = 'puscene';
    const UPYUN_USER = 'puscene';
    const UPYUN_PASS = 'puscenepuscene';
    const DOMAIN_URL = 'img1.mwee.cn';
    protected $extension = [
        'jpg',
        'gif',
        'png',
        'jpeg',
        'image/jpeg',
        'image/gif',
        'image/pjpeg',
        'image/png',
        'image/nbmp',
        'image/x-ms-bmp',
        'application/vnd.android.package-archive',
        'application/octet-stream',
        'apk'
    ];
    protected $size = 500000000;  //10M

    public function uploadify($file,$num='')
    {
        $this->checkFileError($file);
        if($num){
            $arr = $this->uploadImg($file);
        }else{
            $arr = $this->uploadPic($file);
        }
        return $arr;
    }

    public function uploadifyFile($file)
    {
        $this->checkFileError($file);
        $arr = $this->uploadFile($file);
        return $arr;
    }


    private function checkFileError($file)
    {

        //判断错误级别
        switch ($file['error']) {
            case 1:
                $this->getReturnData(1, '文件大小超出了服务器的空间大小');
                break;
            case 2:
                $this->getReturnData(2, '要上传的文件大小超出浏览器限制');
                break;
            case 3:
                $this->getReturnData(3, '文件仅部分被上传');
                break;
            case 4:
                $this->getReturnData(4, '没有找到要上传的文件');
                break;
            case 5:
                $this->getReturnData(5, '服务器临时文件夹丢失');
                break;
            case 6:
                $this->getReturnData(6, '文件写入到临时文件夹出错 ');
                break;
        }
        //判断后缀名

        if (!in_array(pathinfo($file['name'], PATHINFO_EXTENSION), $this->extension)) {
            $this->getReturnData(7, '不支持该文件上传');
        }

        //判断大小
        if ($file['size'] > $this->size) {
            $this->getReturnData(8, '上传图片尺寸过大');
        }
    }

    private function uploadPic($file)
    {
        $upYun = new \Common\Libiary\UpYun(self::UPYUN_BUCKET, self::UPYUN_USER, self::UPYUN_PASS);
        $path = sprintf("/upload/%s/%s.%s", date('Ym/dH'), uniqid(), pathinfo($file ['name'], PATHINFO_EXTENSION));
        $r = fopen($file ['tmp_name'], 'rb');
        $upYun->writeFile($path, $r, true);
        return 'http://' . self::DOMAIN_URL . $path;
    }

    private function uploadImg($file)
    {
        $upYun = new \Common\Libiary\UpYun(self::UPYUN_BUCKET, self::UPYUN_USER, self::UPYUN_PASS);
        $path = sprintf("/upload/%s/%s.%s", date('Ymd'), uniqid(), pathinfo($file ['name'], PATHINFO_EXTENSION));
        $r = fopen($file ['tmp_name'], 'rb');
        $upYun->writeFile($path, $r, true);
        return $path;
    }

    private function uploadFile($file)
    {

        $upYun = new \Common\Libiary\UpYun(self::UPYUN_BUCKET, self::UPYUN_USER, self::UPYUN_PASS);
        $path = sprintf("/upload/%s/%s", date('Ym/dH'), $file ['name']);
        $r = fopen($file ['tmp_name'], 'rb');
        $upYun->writeFile($path, $r, true);
        return 'http://' . self::DOMAIN_URL . $path;
    }

    private function getReturnData($errno, $message,$list=[])
    {
        $data = [
            'errno' => $errno,
            'message' => $message,
            'data' => $list
        ];
        die(json_encode($data));
    }

}